package com.jic.data.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.model.entity.MemberAgeSts;
import com.jic.data.model.entity.MemberLevelPointsStsBase;
import com.jic.data.model.vo.request.MemberAgeStsRequest;
import com.jic.data.model.vo.response.MemberAgeStsResponse;
import com.jic.data.service.MemberAgeStsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: 建投数据科技(山东)有限公司
 * @date: 2020-05-13 17:20:56
 */
@RestController
@Slf4j
@RequestMapping("/memberAgeSts")
@Api(tags = " [会员年龄分布]控制类-PC")
public class MemberAgeStsController {
    @Autowired
    private MemberAgeStsService service;

    /**
     * 新增记录
     *
     * @param request 新增[会员年龄分布]请求参数
     * @return MemberAgeStsResponse 新增[会员年龄分布]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "新增[会员年龄分布]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody MemberAgeStsRequest request) throws Throwable {
        MemberAgeSts memberAgeSts = new MemberAgeSts();
        BeanUtils.copyProperties(request, memberAgeSts);
        long flag = service.save(memberAgeSts);
        return RestResult.success(flag);
    }

    /**
     * 新增或修改记录
     *
     * @param request 新增或修改[会员年龄分布]请求参数
     * @return MemberAgeStsResponse 新增或修改[会员年龄分布]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "保存或更新[会员年龄分布]")
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public RestResult<Long> saveOrUpdate(@Validated @RequestBody MemberAgeStsRequest request) throws Throwable {
        MemberAgeSts memberAgeSts = new MemberAgeSts();
        BeanUtils.copyProperties(request, memberAgeSts);
        long flag = service.saveOrUpdate(memberAgeSts);
        return RestResult.success(flag);
    }

    /**
     * 删除记录
     *
     * @param request 删除[会员年龄分布]请求参数
     * @return MemberAgeStsResponse 删除[会员年龄分布]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "删除[会员年龄分布]记录")
    @PostMapping("/delete")
    @ResponseBody
    public RestResult<Long> delete(@RequestBody MemberAgeStsRequest request) throws Throwable {
        MemberAgeSts memberAgeSts = new MemberAgeSts();
        BeanUtils.copyProperties(request, memberAgeSts);
        long flag = service.deleteByPrimaryKeyLogically(memberAgeSts);
        return RestResult.success(flag);
    }

    /**
     * 修改记录
     *
     * @param request 修改[会员年龄分布]请求参数
     * @return MemberAgeStsResponse 修改[会员年龄分布]响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @ApiOperation(value = "修改[会员年龄分布]记录")
    @PostMapping("/update")
    @ResponseBody
    public RestResult<Long> update(@RequestBody MemberAgeStsRequest request) throws Throwable {
        MemberAgeSts memberAgeSts = new MemberAgeSts();
        BeanUtils.copyProperties(request, memberAgeSts);
        long flag = service.updateByPrimaryKeySelective(memberAgeSts);
        return RestResult.success(flag);
    }

    /**
     * 单表查询详情信息
     *
     * @param request 查询[会员年龄分布]详情请求参数
     * @return MemberAgeStsResponse 查询[会员年龄分布]详情响应参数
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:04
     */
    @PostMapping("/getByPrimaryKey")
    @ApiOperation(value = "查询[会员年龄分布]详情")
    @ResponseBody
    public RestResult<MemberAgeStsResponse> getByPrimaryKey(@Validated @RequestBody MemberAgeStsRequest request) {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        if (request != null && request.getId() != null) {

            MemberAgeSts memberAgeSts = new MemberAgeSts();
            BeanUtils.copyProperties(request, memberAgeSts);
            MemberAgeSts result = service.getByPrimaryKey(memberAgeSts);
            log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
            if (result == null) {
                return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
            }
            MemberAgeStsResponse bo = new MemberAgeStsResponse();
            BeanUtils.copyProperties(result, bo);
            return RestResult.success(bo);
        }
        log.info("查询详情请求结束");
        return RestResult.success(new MemberAgeStsResponse());
    }

    /**
     * 分页查询
     *
     * @param request 分页查询请求参数
     * @return 分页列表
     * @throws Throwable 业务异常和系统异常
     */
    @PostMapping("/listPage")
    @ApiOperation(value = " 分页查询列表 ")
    @ResponseBody
    public RestResult<PageInfo<MemberAgeSts>> listPage(@RequestBody MemberAgeStsRequest request) throws Throwable {
        MemberAgeSts param = new MemberAgeSts();
        BeanUtils.copyProperties(request, param);
        return RestResult.success(service.listPageBySelective(param, request.getPageNum(), request.getPageSize()));
    }

    /**
     * MEMBER_AGE_STS 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/listAll")
    @ApiOperation(value = "不分页查询 [会员年龄分布] ")
    @ResponseBody
    public RestResult<List<MemberAgeStsResponse>> listMemberAgeStsAllRecord(@RequestBody MemberAgeStsRequest request) throws Throwable {
        MemberAgeSts memberAgeSts = new MemberAgeSts();
        BeanUtils.copyProperties(request, memberAgeSts);
        List<MemberAgeSts> list = service.listAllRecord(memberAgeSts);
        List<MemberAgeStsResponse> responseList = list.stream().map(temp -> {
            MemberAgeStsResponse bo = new MemberAgeStsResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }
    /**
     * MEMBER_AGE_STS 查询列表（不分页）
     *
     * @param request 前端请求参数
     */
    @PostMapping("/getAgeDistributionOfMember")
    @ApiOperation(value = "不分页查询 [会员年龄分布] ")
    @ResponseBody
    public RestResult<Map<String,Integer>> getAgeDistributionOfMember(@RequestBody MemberAgeSts request) throws Throwable {
        MemberAgeSts memberAgeSts = new MemberAgeSts();
        BeanUtils.copyProperties(request, memberAgeSts);
        Map<String,Integer>  map = service.getAgeDistributionOfMember(memberAgeSts);
        return RestResult.success(map);
    }

}

